/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons.impl;

import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfWorkitem;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.DfList;
import com.documentum.fc.common.IDfList;

/**
 * Abstract workflow method.
 */
public abstract class AbstractWorkflowMethod extends AbstractMethod {

	/** The workitem id key. */
	public static final String KEY_WORKITEM_ID = "packageId";
	
	/** Return status - success. */
	public static final boolean STATUS_SUCCESS = true;

	/** Return status - failure. */
	public static final boolean STATUS_FAILURE = false;

	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.AbstractMethod#executeMethod()
	 */
	@Override
	public final int executeMethod() {
		return executeWorkflowMethod();
	}
	
	/**
	 * Executes the workflow method.
	 * @return The return status
	 */
	public abstract int executeWorkflowMethod();
	
	/**
	 * Acquires the workitem, resuming the workitem first if necessary.
	 * @param workitem The workitem
	 * @throws DfException The repository exception
	 */
	public void acquire(final IDfWorkitem workitem) throws DfException {
		switch (workitem.getRuntimeState()) {
		case IDfWorkitem.DF_WI_STATE_PAUSED:
			workitem.resume();
		case IDfWorkitem.DF_WI_STATE_DORMANT:
			workitem.acquire();
			break;
		}
	}

	/**
	 * Sets the next activity to be either the first forward or reject
	 * activity. Remember to enable the "Allow performer to set next activity"
	 * option.
	 * @param workitem The workitem
	 * @param status The status (true = follow forward activity, false = follow
	 * reject activity)
	 * @throws DfException The repository exception
	 */
	public void setNextDefaultActivity(final IDfWorkitem workitem,
								       final boolean status)
		throws DfException {
		
		IDfList activities = null;
		
		if (status) {
			activities = workitem.getForwardActivities();
		} else {
			activities = workitem.getRejectActivities();
		}
		
		final IDfList nextActivities = new DfList();
		nextActivities.append(activities.get(0));
		workitem.setOutputByActivities(nextActivities);
	}
	
	/**
	 * Fetches the workitem from the repository.
	 * @param workitemId The workitem id
	 * @return The workitem
	 */
	public IDfWorkitem fetchWorkitem(final IDfSession session,
									 final String workitemId)
		throws DfException {
		return (IDfWorkitem) session.getObject(new DfId(workitemId));
	}
	
	/**
	 * Gets the workitem id.
	 * @return The workitem id
	 */
	public String getWorkitemId() {
		return getParameterValue(KEY_WORKITEM_ID);
	}
}
